//
//  RootViewController.m
//  GPS&Map
//
//  Created by  江志磊 on 15-3-3.
//  Copyright (c) 2015年 江 志磊. All rights reserved.
//

#import "RootViewController.h"
#import "MyPin.h"
@interface RootViewController ()<CLLocationManagerDelegate>
{
    //封装了GPS定位功能,定位要保证手机能联网,定位信息一般通过wifi中手机ip地址或者3g、4g运营商的蜂窝基站获得
    CLLocationManager *_locationManager;
    //苹果地图视图
    MKMapView *_mapView;
    
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化
    _locationManager = [[CLLocationManager alloc] init];
    //设置定位的频率
    //kCLLocationAccuracyBest 时时定位，最精确，但最耗电
    _locationManager.distanceFilter = kCLLocationAccuracyBest;
    //设置代理
    _locationManager.delegate = self;
    //开启定位功能
    [_locationManager startUpdatingLocation];
    //关闭定位功能
    //[_locationManager stopUpdatingLocation];
    // Do any additional setup after loading the view.
    _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    //设置代理
    _mapView.delegate = self;
    
    //一般需要给地图设置中心位置坐标,并设置地图的缩放系数,决定一屏，地图显示的区域
    CLLocationCoordinate2D lc2d = CLLocationCoordinate2DMake(40.035139, 116.311655);
    //设置地图的缩放系数,一般值在(0.01-0.05左右)
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01,0.01);
    //经纬度坐标和缩放系数，决定了地图显示的区域
    //MKCoordinateRegion 地图显示区域的结构体
    MKCoordinateRegion region = MKCoordinateRegionMake(lc2d, span);
    //让地图基于特定的区域显示
    [_mapView setRegion:region];
    [self.view addSubview:_mapView];
    //_mapView.showsUserLocation = YES;
    //一般中心的坐标，或者特定的坐标，通过大头针的方式，呈现在地图上
    //创建一组大头针，显示在地图上
    for (NSInteger i=0; i<10; i++) {
        CLLocationCoordinate2D lc2d = CLLocationCoordinate2DMake(40.035139+0.01*i,116.311655+0.01*i);
        MyPin *pin = [[MyPin alloc] initWithTitle:@"主标题" subTitle:@"副标题" lc2d:lc2d];
        //将大头针对象，添加到地图上
        [_mapView addAnnotation:pin];
    }
    //向地图添加长按手势，实现长按地图某一位置，该位置用大头针标注
    UILongPressGestureRecognizer *lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    //将长按手势添加到地图上
    [_mapView addGestureRecognizer:lp];
}
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{

    NSLog(@"*****更新");

}
- (void)longPressed:(UILongPressGestureRecognizer *)lp{
    //lp.state  一个手势的生命周期began-changed-ended
    //我们一般在手势处于began和changed状态时做后续操作
    if (lp.state==UIGestureRecognizerStateBegan||lp.state==UIGestureRecognizerStateChanged) {
        //取到手势在地图上长按的点坐标
        CGPoint pt = [lp locationInView:_mapView];
        //利用地图的方法，将点坐标转化成地图上的经、纬度坐标
        //convertPoint 要转化的点坐标
        //toCoordinateFromView 点坐标来源于哪个视图
        CLLocationCoordinate2D lc2d  = [_mapView convertPoint:pt toCoordinateFromView:_mapView];
        MyPin *pin =[[MyPin alloc] initWithTitle:@"新添加" subTitle:@"新添加副标题" lc2d:lc2d];
        [_mapView addAnnotation:pin];
    }
}

#pragma mark - MapViewDelegate
//定制大头针顶部视图显示的方法
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //mapView上有对大头针视图的重用队列
    static NSString *annIde = @"pin";
    //如果大头针是id<MKAnnotation> 类型,顶部视图用MKPinAnnotationView
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annIde];
    if (pinView==nil) {
        //创建
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annIde];
        //可以定制视图，左侧和右侧的附加显示
        UIView *left = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,30)];
        left.backgroundColor = [UIColor purpleColor];
        //左侧视图显示
        pinView.leftCalloutAccessoryView =left;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        //设置右侧视图显示
        pinView.rightCalloutAccessoryView = btn;
        //是否允许显示顶部视图
        pinView.canShowCallout = YES;
    }
    return pinView;
}



#pragma mark - CLLocationManagerDelegate
//iOS6之前，比较直观的代理方法
//CLLocation 位置类，里面带有用户经、纬度等位置信息
//- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//}

//locations 数组，里面只有一个CLLocation的对象，对象中带有用户最新的位置信息
//定位频率设置为best，此方法会被时时调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    //拿到位置对象
    CLLocation *lc = locations[0];
    //拿到经纬度结构体
    CLLocationCoordinate2D lc2d = lc.coordinate;
    //获取到经、炜度
    CLLocationDegrees lat = lc2d.latitude;
    CLLocationDegrees longt = lc2d.longitude;
    NSLog(@"lat:%f longt:%f",lat,longt);

}
//当定位失败，或者用户不允许软件访问位置信息时,触发此方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"error:%@",error.localizedDescription);
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
